/*
 * @Author: your name
 * @Date: 2021-01-07 17:30:30
 * @LastEditTime: 2022-02-01 16:27:43
 * @LastEditors: 晟松
 * @Description: In User Settings Edit
 * @FilePath: \volunteer-vue3-admin\src\store\modules\app.js
 */
export default {
    namespaced: true,
    state: {
        slider: {
            opened: JSON.parse(sessionStorage.getItem("opened")),
        },
        token: sessionStorage.getItem("token") || "",
        menuList: null,
        permissionList: [],
        uname: sessionStorage.getItem("uname") || "",
        user: {},
        identity: "",
        tagViews: [],
        schoolAdmin: false, //表示是否是校级管理员
    },
    mutations: {
        TOGGLE_SLIDER(state) {
            state.slider.opened = !state.slider.opened;
            sessionStorage.setItem("opened", JSON.stringify(state.slider.opened));
        },
        SET_TOKEN(state, token) {
            state.token = token;
            sessionStorage.setItem("token", state.token);
        },
        SET_UNAME(state, uname) {
            state.uname = uname;
            sessionStorage.setItem("uname", state.uname);
        },
        SET_IDENTITY(state, identity) {
            state.identity = identity;
            sessionStorage.setItem("identity", identity);
        },
        SET_USER(state, user) {
            state.user = user;
            sessionStorage.setItem("user", JSON.stringify(user));
        },
        SET_MENU_LIST(state, menuList) {
            state.menuList = menuList;
        },
        SET_PERMISSION_LIST(state, permissionList) {
            state.permissionList = permissionList;
        },
        SET_TAG_VIEWS(state, tagViews) {
            state.tagViews = tagViews;
        },
        set_school_admin(state, isSchoolAdmin) {
            state.schoolAdmin = isSchoolAdmin;
        },
        ADD_TAG_VIEWS(state, tagView) {
            if (
                !state.tagViews.some((tag) => {
                    if (tag.fullPath == tagView.fullPath) {
                        return true;
                    }
                    return false;
                })
            ) {
                state.tagViews.push(tagView);
            }
        },
    },
    actions: {
        toggle_slider({ commit }) {
            commit("TOGGLE_SLIDER");
        },
        set_tag_views({ commit }, tagViews) {
            commit("SET_TAG_VIEWS", tagViews);
        },
        set_token({ commit }, token) {
            commit("SET_TOKEN", token);
        },
        set_uname({ commit }, uname) {
            commit("SET_UNAME", uname);
        },
        set_school_admin({ commit }, isSchoolAdmin) {
            commit("set_school_admin", isSchoolAdmin);
        },
        set_identity({ commit }, identity) {
            commit("SET_IDENTITY", identity);
        },
        set_user({ commit }, user) {
            commit("SET_USER", user);
        },
        set_menu_list({ commit }, menuList) {
            commit("SET_MENU_LIST", menuList);
        },
        set_permission_List({ commit }, menuList) {
            // XE: https://x-extends.gitee.io/xe-utils/#/
            // filterTree (obj, iterate[, options, context]) 从树结构中根据回调过滤数据，这里选取type==1的项
            let allMenus = XE.filterTree(menuList, (item) => item.type == 1);
            let permissionList = [];
            allMenus.forEach((item) => {
                if (item.children && item.children.length > 0) {
                    item.children.forEach((menu) => {
                        permissionList.push(`${item.url}/${item.id}/${menu.menu}`);
                    });
                }
            });
            commit("SET_PERMISSION_LIST", permissionList);
        },
    },
};
